package com.yuanchu.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.DateFormatUtils;
import com.yuanchu.common.utils.PageUtils;
import com.yuanchu.common.utils.Query;
import com.yuanchu.goods.dao.YoshopGoodsServiceDao;
import com.yuanchu.goods.dto.GoodsServiceDto;
import com.yuanchu.goods.entity.YoshopGoodsServiceEntity;
import com.yuanchu.goods.entity.YoshopGoodsServiceRelEntity;
import com.yuanchu.goods.service.YoshopGoodsServiceRelService;
import com.yuanchu.goods.service.YoshopGoodsServiceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("yoshopGoodsServiceService")
public class YoshopGoodsServiceServiceImpl extends ServiceImpl<YoshopGoodsServiceDao, YoshopGoodsServiceEntity> implements YoshopGoodsServiceService {

    @Resource
    private YoshopGoodsServiceRelService goodsServiceRelService;

    @Override
    public PageUtils queryGoodsService(Map<String, Object> params) {
        QueryWrapper<YoshopGoodsServiceEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank((String) params.get("search"))){
            queryWrapper.like("name", (String) params.get("search"));
        }
        queryWrapper.eq("is_delete", 0);
        IPage<YoshopGoodsServiceEntity> page = this.page(
                new Query<YoshopGoodsServiceEntity>().getPage(params), queryWrapper);
        if (!page.getRecords().isEmpty()){
            page.getRecords().forEach(goodsServiceEntity -> {
                goodsServiceEntity.setCreateTime(DateFormatUtils.formatDatetime(goodsServiceEntity.getCreateTime()));
                goodsServiceEntity.setUpdateTime(DateFormatUtils.formatDatetime(goodsServiceEntity.getUpdateTime()));
            });
        }
        return new PageUtils(page);
    }

    @Override
    public void addGoodsService(GoodsServiceDto goodsService, int storeId) {
        YoshopGoodsServiceEntity goodsServiceEntity = new YoshopGoodsServiceEntity();
        BeanUtils.copyProperties(goodsService.getForm(), goodsServiceEntity);
        goodsServiceEntity.setStoreId(storeId);
        goodsServiceEntity.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        goodsServiceEntity.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        this.save(goodsServiceEntity);
    }

    @Override
    public void deleteGoodsServiceById(Integer serviceId, int storeId) {
        // 修改字段is_delete为1
        this.update(new UpdateWrapper<YoshopGoodsServiceEntity>()
                .eq("service_id", serviceId).eq("store_id", storeId)
                .set("is_delete", 1).set("update_time", System.currentTimeMillis() / 1000)
        );
        // this.remove(new UpdateWrapper<YoshopGoodsServiceEntity>()
        //         .eq("service_id", serviceId).eq("store_id", storeId));

    }

    @Override
    public void editGoodsServiceById(GoodsServiceDto goodsService, int storeId) {
        YoshopGoodsServiceEntity goodsServiceEntity = new YoshopGoodsServiceEntity();
        BeanUtils.copyProperties(goodsService.getForm(), goodsServiceEntity);
        goodsServiceEntity.setStoreId(storeId);
        // 获取当前时间
        goodsServiceEntity.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        goodsServiceEntity.setServiceId(goodsService.getServiceId());
        this.updateById(goodsServiceEntity);
    }

    @Override
    public List<YoshopGoodsServiceEntity> queryGoodsAll() {
        QueryWrapper<YoshopGoodsServiceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        return this.list(queryWrapper);
    }

    @Override
    public boolean updateGoodsServiceState(GoodsServiceDto goodsService) {
        if (goodsService.getIsDefault() != null){
            if (goodsService.getIsDefault() == 1){
                // 修改字段为0
                return this.update(new UpdateWrapper<YoshopGoodsServiceEntity>()
                        .eq("service_id", goodsService.getServiceId())
                        .set("is_default", 0)
                        .set("update_time", System.currentTimeMillis() / 1000)
                );
            } else if (goodsService.getIsDefault() == 0){
                // 修改字段为1
                return this.update(new UpdateWrapper<YoshopGoodsServiceEntity>()
                        .eq("service_id", goodsService.getServiceId())
                        .set("is_default", 1)
                        .set("update_time", System.currentTimeMillis() / 1000)
                );
            }
        }
        if (goodsService.getStatus() != null){
            if (goodsService.getStatus() == 1){
                // 修改字段为0
                return this.update(new UpdateWrapper<YoshopGoodsServiceEntity>()
                        .eq("service_id", goodsService.getServiceId())
                        .set("status", 0)
                        .set("update_time", System.currentTimeMillis() / 1000)
                );
            } else if (goodsService.getStatus() == 0){
                // 修改字段为1
                return this.update(new UpdateWrapper<YoshopGoodsServiceEntity>()
                        .eq("service_id", goodsService.getServiceId())
                        .set("status", 1)
                        .set("update_time", System.currentTimeMillis() / 1000)
                );
            }
        }
        return false;
    }

    @Override
    public List<YoshopGoodsServiceEntity> queryGoodsServiceByGoods(Integer goodsId) {
        LambdaQueryWrapper<YoshopGoodsServiceRelEntity> rel_lqw = new LambdaQueryWrapper<>();
        rel_lqw.eq(YoshopGoodsServiceRelEntity::getGoodsId, goodsId);
        rel_lqw.select(YoshopGoodsServiceRelEntity::getServiceId);
        List<YoshopGoodsServiceRelEntity> serviceRelEntities = goodsServiceRelService.list(rel_lqw);
        if (serviceRelEntities.isEmpty()){
            return Collections.emptyList();
        }
        List<Integer> serviceIds = serviceRelEntities.stream().map(YoshopGoodsServiceRelEntity::getServiceId).collect(Collectors.toList());
        if (serviceIds.isEmpty()){
            return Collections.emptyList();
        }
        LambdaQueryWrapper<YoshopGoodsServiceEntity> ser_lqw = new LambdaQueryWrapper<>();
        ser_lqw.eq(YoshopGoodsServiceEntity::getIsDelete, 0);
        ser_lqw.in(YoshopGoodsServiceEntity::getServiceId, serviceIds);

        return this.list(ser_lqw);
    }


}